package com.tianzeculture.stagebao.activity.artist;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.base.BaseActivity;
import com.tianzeculture.stagebao.bean.PersonnelPoolBean;
import com.tianzeculture.stagebao.bean.ResultBean;
import com.tianzeculture.stagebao.bean.WorkTag;
import com.tianzeculture.stagebao.net.TagRestUsage;
import com.tianzeculture.stagebao.network.Request;
import com.tianzeculture.stagebao.network.enity.MSG;
import com.tianzeculture.stagebao.util.StatusBarUtil;
import com.tianzeculture.stagebao.util.Util;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/7/4 0004.
 * 加入人才库-个人标签
 */

public class ArtistEditTagActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvStyleTag)
    TextView tvStyleTag;
    @BindView(R.id.tvOutwardTag)
    TextView tvOutwardTag;
    @BindView(R.id.tvBodyTypeTag)
    TextView tvBodyTypeTag;
//    @BindView(R.id.tvCharmTag)
//    TextView tvCharmTag;

    @BindView(R.id.layoutStyleTag)
    LinearLayout layoutStyleTag;
    @BindView(R.id.layoutOutwardTag)
    LinearLayout layoutOutwardTag;
    @BindView(R.id.layoutBodyTypeTag)
    LinearLayout layoutBodyTypeTag;
    //    @BindView(R.id.layoutCharmTag)
//    LinearLayout layoutCharmTag;
    @BindView(R.id.btnSave)
    Button btnSave;

    @Request
    TagRestUsage tagRestUsage = null;

    List<WorkTag> styleNote = null;
    List<WorkTag> appearanceNote = null;
    List<WorkTag> shapeNote = null;
    List<WorkTag> charmPosition = null;

    List<WorkTag> checkStyleNote = null;
    List<WorkTag> checkAppearanceNote = null;
    List<WorkTag> checkShapeNote = null;
    List<WorkTag> checkCharmPosition = null;

    String style;
    String outward;
    String bodytype;
    String charm;

    @Override
    public void initUI() {
        super.initUI();
        StatusBarUtil.setWindowStatusBarColor(this, R.color.colorGray_F8);
        tvTitle.setText(getString(R.string.person_tag));
    }

    @Override
    protected void onResume() {
        super.onResume();
        tagRestUsage.tagList(101);
    }

    @OnClick({R.id.tvStyleTag, R.id.tvOutwardTag, R.id.tvBodyTypeTag, R.id.btnSave})
    public void onClick(View view) {
        Intent intent = new Intent(this, HotTagActivity.class);
        switch (view.getId()) {
            case R.id.tvStyleTag://风格标签
                intent.putExtra("tagType", 0);//0风格标签 1外貌标签 2体型标签 3魅力部位
                intent.putExtra("Tag", (Serializable) styleNote);
                intent.putExtra("checkTag", (Serializable) checkStyleNote);
                startActivityForResult(intent, Util.HOT_TAG);
                break;
            case R.id.tvOutwardTag://外貌标签
                intent.putExtra("tagType", 1);//0风格标签 1外貌标签 2体型标签 3魅力部位
                intent.putExtra("Tag", (Serializable) appearanceNote);
                intent.putExtra("checkTag", (Serializable) checkAppearanceNote);
                startActivityForResult(intent, Util.HOT_TAG);
                break;
            case R.id.tvBodyTypeTag://体型标签
                intent.putExtra("tagType", 2);//0风格标签 1外貌标签 2体型标签 3魅力部位
                intent.putExtra("Tag", (Serializable) shapeNote);
                intent.putExtra("checkTag", (Serializable) checkShapeNote);
                startActivityForResult(intent, Util.HOT_TAG);
                break;
//            case R.id.tvCharmTag://魅力部位
//                intent.putExtra("tagType", 3);//0风格标签 1外貌标签 2体型标签 3魅力部位
//                intent.putExtra("Tag", (Serializable) charmPosition);
//                intent.putExtra("checkTag", (Serializable) checkCharmPosition);
//                startActivityForResult(intent, Util.HOT_TAG);
//                break;
            case R.id.btnSave:
                Intent intent1 = new Intent();
                intent1.putExtra("style", style);
                intent1.putExtra("outward", outward);
                intent1.putExtra("bodytype", bodytype);
                intent1.putExtra("charm", charm);
                PersonnelPoolBean bean = new PersonnelPoolBean();
                bean.setType(2);
                bean.setTag1(style);
                bean.setTag2(outward);
                bean.setTag3(bodytype);
                bean.setTag4(charm);
                setResult(RESULT_OK, intent1);
                EventBus.getDefault().post(bean);
                finish();
                break;
        }
    }

    @Override
    public void refreshUI(int taskId, MSG msg) {
        super.refreshUI(taskId, msg);
        if (taskId == 101) {
            ResultBean resultBean = (ResultBean) msg.getObj();
            styleNote = resultBean.getStyleNote();
            appearanceNote = resultBean.getAppearanceNote();
            shapeNote = resultBean.getShapeNote();
            charmPosition = resultBean.getCharmPosition();
        }
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_edit_tag;
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Util.HOT_TAG) {

            Bundle bundle = data.getExtras();
            int tagType = bundle.getInt("tagType");
            switch (tagType) {//0风格标签 1外貌标签 2体型标签 3魅力部位
                case 0:
                    checkStyleNote = (List<WorkTag>) bundle.getSerializable("checkTag");
                    style = getString(checkStyleNote);
                    updateUI(layoutStyleTag, checkStyleNote);
                    break;
                case 1:
                    checkAppearanceNote = (List<WorkTag>) bundle.getSerializable("checkTag");
                    outward = getString(checkAppearanceNote);
                    updateUI(layoutOutwardTag, checkAppearanceNote);
                    break;
                case 2:
                    checkShapeNote = (List<WorkTag>) bundle.getSerializable("checkTag");
                    bodytype = getString(checkShapeNote);
                    updateUI(layoutBodyTypeTag, checkShapeNote);
                    break;
//                case 3:
//                    checkCharmPosition = (List<WorkTag>) bundle.getSerializable("checkTag");
//                    charm = getString(checkCharmPosition);
//                    updateUI(layoutCharmTag, checkCharmPosition);
//                    break;
            }

        }
    }

    private String getString(List<WorkTag> list) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            if (i == list.size() - 1) {
                sb.append(list.get(i).getName());
                break;
            }
            sb.append(list.get(i).getName());
            sb.append(",");
        }
        return sb.toString();
    }

    private void updateUI(LinearLayout layout, List<WorkTag> tags) {
        layout.removeAllViews();
        for (int i = 0; i < tags.size(); i++) {
            View view = LayoutInflater.from(this).inflate(R.layout.item_work_tag, null);
            CheckBox boxWorkTag = view.findViewById(R.id.boxWorkTag);
            boxWorkTag.setChecked(true);
            boxWorkTag.setClickable(false);
            boxWorkTag.setText(tags.get(i).getName());
            view.setPadding(0, 0, 25, 0);
            layout.addView(view);
        }
    }

}
